import { createRouter, createWebHashHistory } from "vue-router";

// 导入路由页面
import HomeView from "@/views/HomeView.vue";

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      meta: {
        isLogin: true,
        showTabbar: true
      },
      component: HomeView
    },
    {
      path: "/sort",
      name: "sort",
      meta: {
        isLogin: true,
        showTabbar: true
      },
      component: () => import("../views/SortView.vue")
    },
    {
      path: "/cart",
      name: "cart",
      meta: {
        isLogin: false,
        showTabbar: true
      },
      component: () => import("../views/CartView.vue")
    },
    {
      path: "/my",
      name: "my",
      meta: {
        isLogin: false,
        showTabbar: true
      },
      component: () => import("../views/MyView.vue")
    },
    {
      path: "/detail",
      name: "detail",
      meta: {
        isLogin: true
      },
      component: () => import("../views/DetailView.vue")
    },
    {
      path: "/login",
      name: "login",
      meta: {
        isLogin: true
      },
      component: () => import("../views/LoginView.vue")
    },
    {
      path: "/register",
      name: "register",
      meta: {
        isLogin: true
      },
      component: () => import("../views/RegisterView.vue")
    },
    {
      path: "/edit",
      name: "edit",
      meta: {
        isLogin: true
      },
      component: () => import("../views/MyEditView.vue")
    },
    {
      path: "/:pathMatch(.*)",
      name: "NotFound",
      meta: {
        isLogin: true
      },
      component: () => import("../views/NotFound.vue")
    }
  ]
});
// 全局路由守卫
router.beforeEach((to) => {
  if (to.meta.isLogin == true) {
    return true;
  } else {
    if (localStorage.getItem("token")) {
      return true;
    } else {
      showNotify({ type: "warning", message: "请先登录！" });
      //强制跳转到登录页
      return { path: "/login" };
    }
  }
});

export default router;
